﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
    public class AccessoriesDAO
    {
        CameraDBEntities db = new CameraDBEntities();
        public List<Accessory> GetListAll()
        {
            return db.Accessories.Where(x => x.accesStatus == true).ToList();
        }
        public List<Accessory> GetListAll_Sort()
        {
            return db.Accessories.Where(x => x.accesStatus == true).OrderByDescending(x=>x.accesId).ToList();
        }
        public List<Accessory> GetListTop10()
        {
            return db.Accessories.Where(x => x.accesStatus == true).Take(10).ToList();
        }
        public Accessory GetById(int id)
        {
            return db.Accessories.Where(x => x.accesId == id && x.accesStatus == true).SingleOrDefault();
        }

        public List<Accessory> GetListSearch(string tukhoa)
        {
            List<Accessory> listkq = new List<Accessory>();
            listkq = db.Accessories.Where(x => x.accesStatus == true).ToList();
            listkq = listkq.FindAll(

                delegate(Accessory math)
                {
                    if (Utils.ConvertToUnSign(math.accesNames.ToLower()).Contains(tukhoa))
                        return true;
                    else
                        return false;
                }
            );
            return listkq;
        }

        public bool Add(Accessory a)
        {
            try
            {
                db.Accessories.Add(a);
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }

        public bool Edit(Accessory a)
        {
            try
            {
                Accessory aedit = new Accessory();
                aedit = db.Accessories.SingleOrDefault(x=>x.accesId== a.accesId);
                aedit.accesNames= a.accesNames;
                aedit.accesImages= a.accesImages;
                aedit.accesUse= a.accesUse;
                aedit.accesDetails= a.accesDetails;
                aedit.accesPrice = a.accesPrice;
                aedit.accesStatus = a.accesStatus;
                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }

        }
        public bool Delete(int id)
        {
            try
            {
                Accessory a = new Accessory();
               
                a = db.Accessories.Where(x => x.accesId  == id).SingleOrDefault();
                db.Accessories.Remove(a);


                db.SaveChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}
